package com.gitee.frank.minispring.utils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * Description:
 *
 * @author: Frank
 * @date: 2018/3/13
 */
public class BeanFactoryUtil {

    /**
     * @param obj
     * @param filedName
     * @return
     */
    public static Method getWriteMethod(Object obj, String filedName) {

        BeanInfo beanInfo;
        Method method = null;
        try {
            beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class);
            PropertyDescriptor pds[] = beanInfo.getPropertyDescriptors();
            if (pds != null) {
                for (PropertyDescriptor pd : pds) {
                    String filed = pd.getName();
                    if (filed.equals(filedName)) {
                        method = pd.getWriteMethod();
                    }
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
        return method;
    }
}
